package com.hangwei.paperless.view;


import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.utils.Density;
import com.twelve.all.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/10/13.
 */

public class MainProgressTitle {

    public void refreshViewByStep(String curExStep) {
        if (TextUtils.isEmpty(curExStep)) {
            currentPosition = 0;
            refreshProgressView(false);
            return;
        }
        String[] split = curExStep.split("-");
        try {
            int mainStep = Integer.parseInt(split[0]);
            if (mainStep > 9) {//算是异常了
                currentPosition = 8;
                refreshProgressView(true);
            } else {
                currentPosition = mainStep;
                refreshProgressView(false);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            currentPosition = 0;
            refreshProgressView(false);
            ToastUtil.showCenterToast("步骤解析错误");
        }
    }

    enum ItemProgressTagKey {
        IndexKey(Integer.MAX_VALUE / 2),
        StatusKey(Integer.MAX_VALUE / 2 + 1),
        ViewHolderKey(Integer.MAX_VALUE / 2 + 2);

        public int getValue() {
            return value;
        }

        private int value;

        ItemProgressTagKey(int i) {
            value = i;
        }
    }

    private OnProgressItemClickListener l;

    public interface OnProgressItemClickListener {
        void onProgressItemClick(int index, boolean completed);
    }

    public void setOnProgressItemClickListener(OnProgressItemClickListener l) {
        this.l = l;
    }

    public LinearLayout getProgressView() {
        return mProgressView;
    }

    private final LinearLayout mProgressView;

    public MainProgressTitle(Context activity) {
        mActivity = activity;
        mProgressView = new LinearLayout(activity);
        for (int i = 0; i < msg.length; i++) {
            View view;
            if (i == 0) {
                if (currentPosition == 0)
                    view = getItemProgress(0, 0, false);
                else view = getItemProgress(0, 0, true);
            } else if (i == msg.length - 1) {
                if (i < currentPosition)
                    view = getItemProgress(0, 0, true);
                else
                    view = getItemProgress(2, i, false);
            } else {
                view = getItemProgress(1, i, false);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            params.width = 0;
            view.setLayoutParams(params);
            itemViews.add(view);
            mProgressView.addView(view);
        }
    }

    private List<View> itemViews = new ArrayList<>();
    private Context mActivity;

    public int getCurrentPosition() {
        return currentPosition;
    }

    private int currentPosition = 0;

    public static final String[] msg = {"基础信息", "接受检查记录单", "分解记录单", "清洗记录单",
            "检查记录单", "子修理工艺流程单", "装配记录单", "测试记录单", "最终检查记录单"};

    /**
     * @param type 0:起始位置，1：中间位置，2：末尾位置
     */
    private View getItemProgress(int type, final int index, final boolean complete) {
        final View inflate = LayoutInflater.from(mActivity).inflate(R.layout.item_progress, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvMsg = (TextView) inflate.findViewById(R.id.tvMsg);
        viewHolder.view2 = (View) inflate.findViewById(R.id.view2);
        viewHolder.view1 = (View) inflate.findViewById(R.id.view1);
        viewHolder.view0 = (View) inflate.findViewById(R.id.view0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        viewHolder.tvMsg.setText(msg[index]);
        Paint paint = new Paint();
        Rect rect = new Rect();
        paint.setTextSize(viewHolder.tvMsg.getTextSize());
        paint.getTextBounds(msg[index], 0, msg[index].length(), rect);
        //未知原因显示不下，添加一个字的宽度
        params.width = rect.width() + Density.sp2px(mActivity, mActivity.getResources().getDimension(R.dimen.SP_10));
        switch (type) {
            case 0:
                params.gravity = Gravity.START;
                viewHolder.tvMsg.setGravity(Gravity.START);
                viewHolder.view0.setVisibility(View.GONE);
                break;
            case 1:
                params.gravity = Gravity.CENTER_HORIZONTAL;
                viewHolder.tvMsg.setGravity(Gravity.CENTER);
                break;
            case 2:
                viewHolder.view2.setVisibility(View.GONE);
                params.gravity = Gravity.END;
                viewHolder.tvMsg.setGravity(Gravity.END);
                break;
        }
        viewHolder.tvMsg.setLayoutParams(params);
        if (index < currentPosition) {
            viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.blue1));
            viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
            viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_blue_shape_12));
            viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
        } else if (index == currentPosition) {
            viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
            viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.blue1));
            if (complete) {
                viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_blue_shape_12));
                viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
            } else {
                viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_gray_shape_12));
                viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
            }
        } else {
            viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.lightGray));
            viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
            viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_gray_shape_12));
            viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
        }
        inflate.setTag(ItemProgressTagKey.IndexKey.getValue(), index);
        inflate.setTag(ItemProgressTagKey.StatusKey.getValue(), complete);
        inflate.setTag(ItemProgressTagKey.ViewHolderKey.getValue(), viewHolder);
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (l != null) {
                    boolean completed = (boolean) inflate.getTag(ItemProgressTagKey.StatusKey.getValue());
                    l.onProgressItemClick(index, completed);
                }
            }
        });
        return inflate;
    }

    /**
     * 进度更新
     * 由于进度是一步一步的更新，所以每次只需要加1
     */
    public void updateProgress() {
        if (currentPosition == msg.length) {
            refreshProgressView(true);
            itemViews.get(itemViews.size() - 1).setTag(ItemProgressTagKey.StatusKey.getValue(), true);
        } else {
            currentPosition += 1;
            itemViews.get(currentPosition - 1).setTag(ItemProgressTagKey.StatusKey.getValue(), true);
            //currentPosition以前的都是完成的
            refreshProgressView(false);
        }
    }

    /**
     * @param allCompleted 所有步骤都完成了就没必要刷新界面了，通常都是false
     */
    private void refreshProgressView(boolean allCompleted) {
        if (allCompleted) {
            for (View view : itemViews) {
                ViewHolder viewHolder = (ViewHolder) view.getTag(ItemProgressTagKey.ViewHolderKey.getValue());
                viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.blue1));
                viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
                viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_blue_shape_12));
                viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
            }
        } else {
            for (int i = 0; i < itemViews.size(); i++) {
                ViewHolder viewHolder = (ViewHolder) itemViews.get(i).getTag(ItemProgressTagKey.ViewHolderKey.getValue());

                if (i < currentPosition) {
                    viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.blue1));
                    viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
                    viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_blue_shape_12));
                    viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
                } else if (i == currentPosition) {
                    viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.blue1));
                    viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.blue1));
                    viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_gray_shape_12));
                    viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
                } else {
                    viewHolder.tvMsg.setTextColor(mActivity.getResources().getColor(R.color.lightGray));
                    viewHolder.view0.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
                    viewHolder.view1.setBackground(mActivity.getResources().getDrawable(R.drawable.dot_gray_shape_12));
                    viewHolder.view2.setBackgroundColor(mActivity.getResources().getColor(R.color.lightGray));
                }
            }
        }
    }

    public static class ViewHolder {
        TextView tvMsg;
        View view0;
        View view1;
        View view2;
    }
}